//index.js
//获取应用实例
let app = getApp(),
	index,title,type,song;

let circle = (deg) => {
	return deg += 180;
};

let pageCof = {
	data:{
		loading: true,
		showContent: false
	},

	// 加载中
	onLoad: function(opt){
		// 加载提示
		wx.showToast({
		  	title: '音乐加载中',
		  	icon: 'loading',
		  	duration: 10000
		});

		// 获取url参数
		index = opt.index || 0;
		type = opt.type,
		title = opt.title;

		// 动态设置页面title
		wx.setNavigationBarTitle({title});
	},

	// 准备完成
	onReady: function(){
		let me = this;
		// 显示顶部加载icon
		wx.showNavigationBarLoading();

		let cb = (album) => {
			let list = album.songlist || [];
			song = list[index];
			// 更新视图
			me.setData({
				song,
				loading: false,
				showContent: true
			});
			// 无限旋转动画
			let deg = 180,
				animate = wx.createAnimation({
				  	duration: 3000,
				  	delay: 0
				});
			// 添加旋转动作
			animate.rotate(180).step();
			// 更新动画视图
			me.setData({
				animate: animate.export()
			});
			// 播放背景音乐
			wx.playBackgroundAudio({
			    dataUrl: song.url,
			    title: song.songname,
			    coverImgUrl: song.albumpic_big,
			    success: () => {
			    	// 隐藏loading弹层
			    	wx.hideToast();
			    	// 隐藏顶部加载icon
					wx.hideNavigationBarLoading();
			    }
			});

			// 动画定时器
			setInterval(function(){
				deg += 180;
				animate.rotate(deg).step()
				me.setData({
					animate: animate.export()
				});
			}, 3000);
		};
		!app.globalData[type] ? app.getData(function(){
			cb(app.globalData[type])
		}) : cb(app.globalData[type]);
	},
	onUnload: function(){
		// 暂停后台音乐
		wx.stopBackgroundAudio()
	},
	// 分享
	onShareAppMessage: () => {
		return {
	      	title: song.songname,
	      	desc: '',
	      	path: '/pages/play/play?type=' + type + '&index=' + index
	    }
	}
};

Page(pageCof);
